﻿
Public Class PurchaseOrderExplorer
    Inherits ExplorerTreeList

    Private EditPanePL As PLBase

    'Public selectedRow As DataRow
    'Public aSelectedGUID As Guid
    Public PoDetail As SystemBoxSDK.PL.CRMSDK.PurchaseOrderDetail_PL

    Public PurchaseOrderID As Guid
    Public PurchaseOrderDetailID As Guid

    'Public aRow As DataRow
    'Public frmCaption As String = Nothing

    Public aLotTransactionHelper As LotTransactionHelper

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
        aLotTransactionHelper = TryCast(Win.WinCtrlBase.CreateInstanceOf(Win.WinCtrlTypeOf.LotTransactionHelper), LotTransactionHelper)
    End Sub

    Public Sub New()
        MyBase.New()
        aLotTransactionHelper = TryCast(Win.WinCtrlBase.CreateInstanceOf(Win.WinCtrlTypeOf.LotTransactionHelper), LotTransactionHelper)
    End Sub

    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        '
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If

        Dim guidEnfant As Guid
        Dim aPLName As String

        Dim DescriptionLang As String = "_PRM"

        Dim aPaneMetaQueryName As String
        Dim Ametaquerie As MetaQuery
        Dim frmCaption As String = "Purchase Order"


        Try
            guidEnfant = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
            aPLName = DirectCast(NodeDataRow("PL"), String)

            aPaneMetaQueryName = DirectCast(NodeDataRow("MetaQuerie"), String)
            Ametaquerie = MetaQuery.GetMetaQueryByName(aPaneMetaQueryName)

            'c'est ici qu'on bind les info du noeud qui articule le Edit PANE
            EditPanePL = PLBase.CreateInstanceOf(aPLName, aPaneMetaQueryName)

            aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = EditPanePL, .SecureGrid = True, .Name = Ametaquerie.Caption, .FormCtrlCD = MyBase.PanelName}
            Select Case FocusedNodeLevel
                Case 0
                    frmCaption = "Purchase Order List"
                    'ShowAll()
                Case 1
                    If NodeDataRow.Table.Columns.Contains("PurchaseOrderCd") Then
                        frmCaption = String.Format("{0}", NodeDataRow("PurchaseOrderCd"))
                    Else
                        frmCaption = "PurchaseOrderCd"
                    End If
                    PurchaseOrderID = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
                    'frmCaption = String.Format("{0}", aRow("PurchaseOrderCd"))

                    If Not Guid.Empty.Equals(guidEnfant) Then
                        EditPanePL.SQLWhere = String.Format("{0} = '{1}'", Ametaquerie.GUIDField, guidEnfant)
                        If AppManager.ApplicationOption.DescriptionColumnIndex = 3 Then
                            DescriptionLang = "_ALT"
                        End If

                        If NodeDataRow.Table.Columns.Contains("Code") AndAlso NodeDataRow.Table.Columns.Contains("Description" & DescriptionLang) Then
                            frmCaption = String.Format("{0} - {1}", NodeDataRow("Code"), NodeDataRow("Description" & DescriptionLang))
                        Else
                            frmCaption = "Code, Description" & DescriptionLang
                        End If

                    End If
                    MainLevelExplorer()
                Case 2
                    If NodeDataRow.Table.Columns.Contains("PrdCd") Then
                        frmCaption = String.Format("{0}", NodeDataRow("PrdCd"))
                    Else
                        frmCaption = "PrdCd"
                    End If
                    PurchaseOrderDetailID = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
                    DetailExplorer()
                Case Else
            End Select

        Catch ex As Exception
            MessageWindow.ShowMessage(ex)

        End Try

        InitGridActionOption(aGridControl)


        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Text = frmCaption
                aFrm.Controls.Add(aGridControl)
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next
        '
        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub ShowAll()
        Me.Cursor = Cursors.WaitCursor
        Me.Cursor = Cursors.Default
    End Sub



    Public Overridable Sub MainLevelExplorer()


        aGridControl.GridActionOptionList.Add(New ActionOption("PrintPO", "ACT_00122", "Print Purchase Order", AddressOf MenuPrintPurchaseOrder, True, True))
        aGridControl.GridActionOptionList.Add(New ActionOption("Process", "ACT_00123", "Process PO (WSvc)", AddressOf aLotTransactionHelper.ProcessPO, True, True, "PurchaseOrder"))
        aGridControl.GridActionOptionList.Add(New ActionOption("RecalTax", "ACT_00188", "Re-Calcul des Taxes 2012", AddressOf RecalcTax2012, True, True, "PurchaseOrder"))

        ' AddHandler aGridControl.InitNewMasterRow, AddressOf InitnewMasterRow
        AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf ProcessCurrentDetailRecord
        AddHandler aGridControl.DetailCellValueChanged, AddressOf DetailCellValueChanged


        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewRow
        AddHandler aGridControl.ValidateMasterRow, AddressOf ValidateRow
        AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateRow
        AddHandler aGridControl.MasterRowNotify, AddressOf RowNotify

    End Sub

    Public Overridable Sub DetailExplorer()
        ' frmCaption = aRow("Description").ToString

        PoDetail = New SystemBoxSDK.PL.CRMSDK.PurchaseOrderDetail_PL("PurchaseOrderDetailGrid")

        'Scoper nouveau PL en haut en haut        
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = PoDetail, .Name = "PurchaseOrderDetailGrid", .FormCtrlCD = PanelName}
        aGridControl.SecureGrid = True
        '
        PoDetail.SQLWhere = String.Format("PurchaseOrderDetailID = '{0}'", PurchaseOrderDetailID)

        'aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = MyBase.PL, .Name = "PurchaseOrderDetailGrid", .FormCtrlCD = PanelName}
        'aGridControl.SecureGrid = True
        ''
        'MyBase.PL.SQLWhere = String.Format("PurchaseOrderDetailId = '{0}'", PurchaseOrderDetailID)

        aGridControl.GridActionOptionList.Add(New ActionOption("Process", "ACT_00124", "Process PO Transaction (WSvc)", AddressOf aLotTransactionHelper.ProcessPOTransaction, True, True, "PurchaseOrderDetail_LotTran"))

        '
        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewRow
        'AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf ProcessRow

        AddHandler aGridControl.ValidateMasterRow, AddressOf ValidateRow
        AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateRow

        AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf ProcessCurrentDetailRecord
        AddHandler aGridControl.DetailCellValueChanged, AddressOf DetailCellValueChanged

        AddHandler aGridControl.DetailCustomRowCellEditForEditing, AddressOf DetailCustomRowCellEditForEditing

    End Sub

    'Private Sub PurchaseOrderExplorer_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    '    aLotTransactionHelper = New LotTransactionHelper
    'End Sub


End Class
